package com.iseastar.dianxiaosan.home.adapter;

import java.util.ArrayList;
import java.util.HashSet;

import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.dianxiaosan.home.parcel.ParcelCheckActivity;
import com.iseastar.dianxiaosan.model.ParcelCheckBean;
import com.iseastar.guojiang.logic.AppHttp;
import com.shouhuobao.station.R;

import droid.frame.activity.base.BaseAdapterWithImage;
import droid.frame.ui.dialog.AppDialog;

public class ParcelCheckAdapter extends BaseAdapterWithImage<ParcelCheckBean> {

	private int num;
	// 收取的点击事件和全部收取时把isFirst和pos 恢复 默认值
	// 判读是否是第一个未收取的包裹
	public boolean isFirst = true;

	// 记录可以收取的包裹的第一个postion
	public int pos = 0;

	private ParcelCheckActivity parcelCheckActivity;

	public ParcelCheckAdapter(ArrayList<ParcelCheckBean> items, ParcelCheckActivity context, ViewGroup viewGroup) {
		super(items, context, viewGroup);
		parcelCheckActivity = context;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.parcel_check_item, parent, false);
			holder.titleLayout = (TextView) convertView.findViewById(R.id.parcel_check_other_title);
			// 根布局
			holder.checkLayout = (LinearLayout) convertView.findViewById(R.id.parcel_check_layout);
			holder.code = (TextView) convertView.findViewById(R.id.parcel_check_code);
			holder.number = (TextView) convertView.findViewById(R.id.check_sender_phone);
			holder.name = (TextView) convertView.findViewById(R.id.check_sender_name);
			holder.img = (ImageView) convertView.findViewById(R.id.parcel_check_photo);
			holder.type = (TextView) convertView.findViewById(R.id.check_goods_type);
			// 已收取包裹的布局
			holder.takeLayout = (LinearLayout) convertView.findViewById(R.id.parcel_take_layout);
			// 收取包裹
			holder.take = (TextView) convertView.findViewById(R.id.parcel_check_finish);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final ParcelCheckBean item = items.get(position);
		// set image
		showImage(item.getParcelImgUrl(), holder.img, null);
		holder.number.setText(Html.fromHtml("<font color=#888888>电话: </font>" + item.getUserPhone()));// 收件人电话
		if(TextUtils.isEmpty(item.getBagCode())||"null".equals(item.getBagCode())){
			holder.code.setText(Html.fromHtml("<font color=#888888>投递编号: </font>" + context.getString(R.string.parcel_nobag_code)));// 投递袋编码
		}else{
			holder.code.setText(Html.fromHtml("<font color=#888888>投递编号: </font>" + item.getBagCode()));// 投递袋编码
		}
		holder.name.setText(Html.fromHtml("<font color=#888888>寄件人: </font>" + item.getUserName()));
		holder.type.setText(Html.fromHtml("<font color=#888888>物品类型: </font>" + item.getCategory()));// 物品类型

		holder.take.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final AppDialog dialog = new AppDialog(context);
				TextView dialogOK = (TextView) dialog.findViewById(R.id.app_dialog_cancel);
				dialogOK.setTextColor(context.getResources().getColor(R.color.dialog_ok));
				dialog.setContent("确定收取包裹?");
				dialog.setCancelClickListener("取消", null);
				dialog.setConfirmClickListener("确定", new OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
						parcelCheckActivity.showLoadingDialog(null);
						AppHttp.getInstance().stationReceiveByIds(item.getId());
					}
				});
				dialog.show();
			}
		});
		if (item.getIsTake() == 0) {
			holder.checkLayout.setBackgroundResource(R.color.white);
			holder.takeLayout.setVisibility(View.GONE);
			holder.take.setVisibility(View.VISIBLE);
			holder.number.setTextColor(getColor(R.color.app_font));
			holder.code.setTextColor(getColor(R.color.app_font));
			holder.type.setTextColor(getColor(R.color.app_font));
			holder.name.setTextColor(getColor(R.color.app_font));
			// 待收取
			if (isFirst) {
				if (position == 0) {
					pos = position + 1;
				} else {
					pos = position;
				}
				isFirst = false;
				if (pos == position) {
					holder.titleLayout.setVisibility(View.VISIBLE);
				} else {
					holder.titleLayout.setVisibility(View.GONE);
				}
			} else {
				if (pos == position) {
					holder.titleLayout.setVisibility(View.VISIBLE);
				} else {
					holder.titleLayout.setVisibility(View.GONE);
				}
			}
		} else {
			holder.titleLayout.setVisibility(View.GONE);
			holder.checkLayout.setBackgroundResource(R.color.black_30);
			holder.takeLayout.setVisibility(View.VISIBLE);
			holder.take.setVisibility(View.GONE);
			holder.number.setText(Html.fromHtml("<font color=#6F8FAF>电话: </font>" + item.getUserPhone()));// 收件人电话
			holder.code.setText(Html.fromHtml("<font color=#6F8FAF>投递袋编号: </font>" + item.getBagCode()));// 投递袋编码
			holder.name.setText(Html.fromHtml("<font color=#6F8FAF>寄件人: </font>" + item.getUserName()));
			holder.type.setText(Html.fromHtml("<font color=#6F8FAF>物品类型: </font>" + item.getCategory()));// 物品类型
			holder.number.setTextColor(getColor(R.color.app_font_blue));
			holder.code.setTextColor(getColor(R.color.app_font_blue));
			holder.type.setTextColor(getColor(R.color.app_font_blue));
			holder.name.setTextColor(getColor(R.color.app_font_blue));
		}
		return convertView;
	}

	/**
	 * 获取待收取包裹的数量
	 * 
	 * @return
	 */
	public HashSet<Integer> getUnTakedSet() {
		HashSet<Integer> idsSet = new HashSet<Integer>();
		for (ParcelCheckBean item : items) {
			if (item.getIsTake() == 0) {
				idsSet.add(item.getId());
			}
		}
		return idsSet;
	}

	private static class ViewHolder {
		TextView code, number, name, type, take, titleLayout;
		ImageView img;// 包裹中寄件照片
		LinearLayout takeLayout, checkLayout;
	}

}
